Often the programs you write will have errors, and will need correction. Although Borland cannot catch your logical errors, such as adding two number when you intended to subtract them, it does point out your syntax errors. A syntax error happens when you write something that the language does not allow. (In English, the sentence "I to go home now," has a syntax error. Although the meaning of the words is clear to most English speakers, the language was not used correctly in expressing it.) Let's add a syntax error to our program and see what happens. Suppose we have the program:
#include int main() { int; cout << "Hey! I wrote my first program!" << endl; = }
This has two syntax errors. The lines with "int;" and "=" are both illegal. When we try to run the program we get the following report:
This tells us that there were four errors in our program (though we know there are really only two), and thus it couldn't be run. When we click on the "OK" button, our only option at this point, Borland opens the Message Window, which gives us more information about our errors:
There are two main ways to use the message window. When the window is active (indicated by the fact that its title bar is blue) pressing the up and down arrow keys will scroll you through the list of errors. At the same time, the line on which the error occurred is highlighted in the program window. Note: 1) not all errors correspond to some particular line, notably 'linker' errors, and 2) the actual place where you made the mistake is not necessarily the same line about which Borland complains! So, when searching for an error, be sure to consider code on previous lines as well as the line for which Borland actually reported the error.
Let's now fix the errors in the program. If you are in the message window, then double-clicking with the mouse on an error, or selecting the error with the arrow keys and hitting ENTER, will move you to the program window and place the cursor at the location of the error. Suppose we double-click on the first error in the list:
The cursor is now at the spot of the first error, we delete the whole line, then return to message window to view our other errors. It is important to note that a single error, in the case, the line with the single equal sign, is reported multiple times in the message window. This is because that single symbol is actually violating several expectations of the Borland compiler at once. If we delete the equal sign as well, our program runs without a hitch.
Let's look at another, very common error. Suppose our program is this:
#include int main() { cout << "Hey! I wrote my first program!" << endl cout << "And here is a second line of output." << endl; }
Notice that we have added a second output statement, and removed the semicolon at the end of the first. When we attempt to run the program we get a single error:
The "statement missing semicolon" error is one of the most common errors encountered, and is easy to fix. Almost without exception, one needs merely to look at the line of code previous to the line on which Borland complained of an error, and add a semicolon ( ; ) where one is lacking. When this is done for the above program, the error is fixed and the program runs without a hitch.